package com.example.demo.controller;

import com.example.demo.entity.Coach;
import com.example.demo.entity.CoachAccount;
import com.example.demo.entity.Response;
import com.example.demo.service.Impl.CoachImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/coach")
@RequiredArgsConstructor
public class CoachController {
    private final CoachImpl coachImpl;

    @GetMapping("/getAllCoach")
    public ResponseEntity<Response> getAllCoach() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getAllCoach",coachImpl.getAllCoach()))
                        .message("获取所有教练信息")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    @PostMapping("/addCoach")
    public ResponseEntity<Response> addCoach(@RequestBody Coach coach){
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("addCoach",coachImpl.addCoach(coach)))
                            .message("新增教练")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            //如果出现异常，打印堆栈信息，并返回一个失败的响应对象
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("新增失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @PostMapping("/addCoachandAccount")
    public ResponseEntity<Response> addCoach(@RequestBody Coach coach,@RequestParam Long coachAccountId){
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("addCoach",coachImpl.addCoach(coach,coachAccountId)))
                            .message("新增教练")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            //如果出现异常，打印堆栈信息，并返回一个失败的响应对象
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("新增失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

    @DeleteMapping("/deleteCoach/{coachId}")
    public ResponseEntity<Response> deleteCoach(@PathVariable Long coachId) {
        try{
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("deleteCoach", coachImpl.deleteCoach(coachId)))
                            .message("删除成功")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("删除失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

    @PostMapping("/updateCoach")
    public ResponseEntity<Response> updateCoach(@RequestBody Coach coach) {
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("updateCoach",coachImpl.updateCoach(coach)))
                            .message("更新教练")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("更新教练失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

    @PostMapping("/filterCoach")
    public ResponseEntity<Response> filterCoach(@RequestBody Coach coach) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("filterCoach",coachImpl.filterCoach(coach)))
                        .message("查找教练")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    @GetMapping("/getCoachByCoachId/{coachId}")
    public ResponseEntity<Response> getCoachByCoachId(@PathVariable("coachId") Long coachId)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getCoachByCoachId",coachImpl.getCoachByCoachId(coachId)))
                        .message("教练ID查找")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    @GetMapping("/getCoachAccountByCoachId/{coachId}")
    public ResponseEntity<Response> getCoachAccountByCoachId(@PathVariable("coachId") Long coachId)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getCoachAccountByCoachId",coachImpl.getCoachAccountByCoachId(coachId)))
                        .message("教练ID查找")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    @PostMapping("/getSelfAccount")
    public ResponseEntity<Response> getSelfAccount(@RequestBody CoachAccount coachAccount)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getSelfAccount",coachImpl.updateSelfAccount(coachAccount)))
                        .message("教练账户更新")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
    @PostMapping("/getCoachBySexOrAge")
    public ResponseEntity<Response> getCoachBySexOrAge(@RequestParam("sex") String sex,
                                                       @RequestParam("age1") String age1,
                                                       @RequestParam("age2") String age2)
    {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("getCoachBySexOrAge",coachImpl.findBySexOrAge(sex,age1,age2)))
                        .message("根据性别年龄筛选教练")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

}
